package com.tech.arno.spicy.JPush;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.UserManager;

import com.google.gson.Gson;
import com.tech.arno.spicy.module.PushMessage;
import com.tech.arno.spicy.ui.activity.HomeActivity;
import com.tech.arno.spicy.ui.activity.LoginActivity;
import com.tech.arno.spicy.util.Util;
import com.util.LogUtil;
import com.util.ResponseEntityToModule;

import cn.jpush.android.api.JPushInterface;

/*
*   @package:     com.tech.arno.spicy.JPush
*   @fileName:    SpicyJPushReceiver
*   @author:    ArnoFrost
*   @date:  2017/12/19 0019 9:35
*   @desc:     Spicy应用极光推送Receiver
*/

public class SpicyJPushReceiver extends BroadcastReceiver {

    public static String PUSH_MESSAGE = "pushMessage";
    public static String FROM_PUSH = "fromPush";


    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_RECEIVED)) {
            //不需要跳转
        } else if (intent.getAction().equals(JPushInterface.ACTION_NOTIFICATION_OPENED)) {
            //需要跳转
            LogUtil.d("Push", bundle.getString(JPushInterface.EXTRA_EXTRA));
            PushMessage message = (PushMessage) ResponseEntityToModule.parseJsonToModule(
//                    bundle.getString(JPushInterface.EXTRA_EXTRA)
                    bundle.getString(JPushInterface.EXTRA_EXTRA)
                    , PushMessage.class);
            LogUtil.d("Push", message.toString());
            if (Util.getCurrentTask(context)) {
                Intent pushIntent = new Intent();
                //启动模式为NEW_TASK
                pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //放入值
                pushIntent.putExtra(PUSH_MESSAGE, message);
                //应用已经启动
                if (message != null
                        && message.messageType.equals("2")
                        && !com.tech.arno.spicy.manager.UserManager.getInstance().hasLogined()) {
                    //如果需要登录,且当前用户未登录 则 跳转登录
                    pushIntent.setClass(context, LoginActivity.class);
                    //放置一个标志
                    pushIntent.putExtra(FROM_PUSH, true);
                } else {
                    //用户已经登录或者不需要登录,直接跳转到消息展示页面
                    pushIntent.setClass(context, PushMessageActivity.class);
//                    pushIntent.putExtra(FROM_PUSH, false);

                }
                //最后启动相应Activity
                context.startActivity(pushIntent);
            } else {
                //应用未启动
                Intent mainIntent = new Intent(context, HomeActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (message != null && message.messageType.equals("2")) {
                    //需要登录 先跳转登录页面
                    Intent loginIntent = new Intent(context, LoginActivity.class);
                    //从推送发出
                    loginIntent.putExtra(FROM_PUSH, true);
                    loginIntent.putExtra(PUSH_MESSAGE, message);
                    //通过数组方式传入intent
                    context.startActivities(new Intent[]{mainIntent, loginIntent});

                } else {
                    //用户已经登录或者不需要登录,直接跳转到消息展示页面 然后返回主页面
                    Intent pushIntent = new Intent(context, PushMessageActivity.class);
                    pushIntent.putExtra(PUSH_MESSAGE, message);
                    context.startActivities(new Intent[]{mainIntent, pushIntent});
                }
            }
        }

    }
}
